home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / cmplib_s.lha / cmplib_src / $asmpass11.P < prev    next >
Text File  |  1990-04-12  |  9KB  |  217 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* $asmpass11.P */
  25.  
  26. /* **********************************************************************
  27. $asmpass11_export([$asm_pass1/6]).
  28.  
  29. $asmpass11_use($blist,[_,_,$member1/2]).
  30. $asmpass11_use($name,[$name/2,_]).
  31. $asmpass11_use($meta,[_,_,$length/2]).
  32. $asmpass11_use($listutil1,[_,_,_,_,_,_,$member2/2,$closetail/1]).
  33. ********************************************************************** */
  34.  
  35. $asm_pass1(AsmInsts, Index, Csym, Lsym, Ntext, Npsc) :-
  36.     $asm_pass1(AsmInsts,Lsym,Csym,0,Ntext),
  37.     $asm_index_pass1(Index, Csym),
  38.     $asm_uniq( Lsym, EPtable ), 
  39.     $closetail( Lsym ),
  40.     $closetail( EPtable ),
  41.     $asmpass1_fillin( EPtable, Csym ),
  42.     $asmpass1_setundef( Csym, Npsc, 0),
  43.     !.
  44.  
  45. $asm_index_pass1([],_).
  46. $asm_index_pass1([pred(_,_)|Rest], Csym) :- $asm_index_pass1(Rest, Csym).
  47. $asm_index_pass1([arglabel(T,Val,Label)|Rest],Csym) :-
  48.     (T ?= c ->
  49.          $member1((Val, 0, _), Csym);
  50.          (T ?= s -> 
  51.              (Val = (Str, Ar), $member1((Str, Ar, _), Csym)) ;
  52.              true
  53.          )
  54.     ),
  55.     $asm_index_pass1(Rest, Csym).
  56.  
  57. $asm_pass1([],_,_,Lc,Lc).
  58. $asm_pass1([Inst| Rest], Lsym, Csym, Lc, NLc):-
  59.      (Inst \= label(_) ->
  60.          ($asm_pass1(Inst,Csym,N),
  61.           Lc0 is Lc + N
  62.      ) ;
  63.      (Inst = label(X),
  64.           $member1((X, Lc), Lsym),
  65.       Lc0 = Lc
  66.      )
  67.     ),
  68.     $asm_pass1(Rest, Lsym, Csym, Lc0, NLc).
  69.  
  70.         
  71. :- mode $asm_pass1(++,?,-).
  72.  
  73. $asm_pass1( label(X),                 _,  0).
  74. $asm_pass1( getpvar(V,R),             _,  8).
  75. $asm_pass1( getpval(V,R),             _,  8).
  76. $asm_pass1( getstrv((S,N),V),Csym,        8) :- $member1((S,N,_),Csym).
  77. $asm_pass1( gettval(R,R1),            _,  8).
  78. $asm_pass1( getcon(C,R),Csym,             8) :- $member1((C,0,_), Csym).
  79. $asm_pass1( getnil(R),                _,  4). 
  80. $asm_pass1( getstr((S,N),R),Csym,         8) :- $member1((S,N,_),Csym).
  81. $asm_pass1( getlist(R),               _,  4).
  82. $asm_pass1( unipvar(V),               _,  4).
  83. $asm_pass1( unipval(V),               _,  4).
  84. $asm_pass1( unitvar(R),               _,  4).
  85. $asm_pass1( unitval(R),               _,  4).
  86. $asm_pass1( unicon(C),Csym,               8) :- $member1((C,0,_),Csym).
  87. $asm_pass1( uninil,                   _,  4).
  88. $asm_pass1( getnumcon(I,R),           _,  8).
  89. $asm_pass1( putnumcon(N,R),           _,  8).
  90. $asm_pass1( putpvar(V,R),             _,  8).
  91. $asm_pass1( putpval(V,R),             _,  8).
  92. $asm_pass1( puttvar(R,R1),            _,  8).
  93. $asm_pass1( putstrv((S,N),V),Csym,        8) :- $member1((S,N,_),Csym).
  94. $asm_pass1( putcon(C,R),Csym,             8) :- $member1((C,0,_),Csym).
  95. $asm_pass1( putnil(R),                _,  4).
  96. $asm_pass1( putstr((S,N),R),Csym,         8) :- $member1((S,N,_),Csym).
  97. $asm_pass1( putlist(R),               _,  4).
  98. $asm_pass1( bldpvar(V),               _,  4).
  99. $asm_pass1( bldpval(V),               _,  4).
  100. $asm_pass1( bldtvar(R),               _,  4).
  101. $asm_pass1( bldtval(R),               _,  4).
  102. $asm_pass1( bldcon(C),Csym,               8) :- $member1((C,0,_),Csym).
  103. $asm_pass1( bldnil,                   _,  4).
  104. $asm_pass1( uninumcon(N),             _,  8). 
  105. $asm_pass1( bldnumcon(N),             _,  8).
  106. $asm_pass1( getfloatcon(I,R),         _,  8).
  107. $asm_pass1( putfloatcon(I,R),         _,  8).
  108. $asm_pass1( unifloatcon(N),           _,  8). 
  109. $asm_pass1( bldfloatcon(N),           _,  8).
  110. $asm_pass1( test_unifiable(R1,R2,R3), _,  8).
  111.  
  112. $asm_pass1( getlist_k(R),                   _,  4).
  113. $asm_pass1( getlist_k_tvar_tvar(R0,R1,R2),  _,  8).
  114. $asm_pass1( getlist_tvar_tvar(R0,R1,R2),    _,  8).
  115. $asm_pass1( getcomma(R),                    _,  4).
  116. $asm_pass1( getcomma_tvar_tvar(R0,R1,R2),   _,  8).
  117.  
  118. $asm_pass1( trymeelse(L,A),           _,  8).
  119. $asm_pass1( retrymeelse(L,A),         _,  8).
  120. $asm_pass1( trustmeelsefail(A),       _,  4).
  121. $asm_pass1( try(L,A),                 _,  8).
  122. $asm_pass1( retry(L,A),               _,  8).
  123. $asm_pass1( trust(L,A),               _,  8).
  124. $asm_pass1( getpbreg(V),              _,  4).
  125. $asm_pass1( gettbreg(R),              _,  4).
  126. $asm_pass1( putpbreg(V),              _,  4).
  127. $asm_pass1( puttbreg(R),              _,  4).
  128.  
  129. $asm_pass1( switchonterm(R,L,L1),     _, 12).
  130. $asm_pass1( arg(I,T,X),               _,  8).
  131. $asm_pass1( arg0(I,T,X),              _,  8).
  132. $asm_pass1( switchonbound(R, L1, L2), _, 12).
  133. $asm_pass1( switchonlist(R,L,L1),     _, 12).
  134.  
  135. $asm_pass1( get_tag(R1,R2),           _,  8).
  136.  
  137. $asm_pass1( movreg(R,R1),             _,  8).
  138. $asm_pass1( negate(R),                _,  4).
  139. $asm_pass1( and(R,R1),                _,  8).
  140. $asm_pass1( or(R,R1),                 _,  8).
  141. $asm_pass1( lshiftl(R,R1),          _,  8).
  142. $asm_pass1( lshiftr(R,R1),          _,  8).
  143. $asm_pass1( addreg(R,R1),             _,  8).
  144. $asm_pass1( subreg(R,R1),             _,  8).
  145. $asm_pass1( mulreg(R,R1),             _,  8).
  146. $asm_pass1( divreg(R,R1),             _,  8).
  147. $asm_pass1( idivreg(R,R1),            _,  8).
  148.  
  149. $asm_pass1( putdval(V,R),             _,  8).
  150. $asm_pass1( putuval(V,R),             _,  8).
  151.  
  152. $asm_pass1( call(P,N,B),Csym,           8) :- 
  153.     N >= 0 -> $member1((P,N,_),Csym) ; true.
  154. $asm_pass1( allocate(_),              _,  4).
  155. $asm_pass1( deallocate,               _,  4).
  156. $asm_pass1( proceed,                  _,  4).
  157. $asm_pass1( execute((P,N)),Csym,          8) :- $member1((P,N,_),Csym).
  158.  
  159. $asm_pass1( calld(P,N,B),Csym,          8) :- 
  160.     N >= 0 -> $member1((P,N,_),Csym) ; true.
  161. $asm_pass1( jump(L),                  _,  8).
  162. $asm_pass1( jumpz(L,R),               _,  8).
  163. $asm_pass1( jumpnz(L,R),              _,  8).
  164. $asm_pass1( jumplt(L,R),              _,  8).
  165. $asm_pass1( jumple(L,R),              _,  8).
  166. $asm_pass1( jumpgt(L,R),              _,  8).
  167. $asm_pass1( jumpge(L,R),              _,  8).
  168.  
  169. $asm_pass1( fail,                     _,  4).
  170. $asm_pass1( noop,                     _,  4).
  171. $asm_pass1( halt,                     _,  4).
  172. $asm_pass1( builtin(S),               _,  4).
  173.  
  174. $asm_pass1( callv(B),                 _,  8).
  175. $asm_pass1( executev,                 _,  4).
  176.  
  177. $asm_pass1( Junk,_,0) :- 
  178.     $umsg(['*** Error in assembly: unknown opcode: ',Junk]).
  179.  
  180. /*    Fill in the values of any symbols which have not been defined
  181. with the value -2. */
  182.  
  183. $asmpass1_setundef([], N, N) :- !.
  184. $asmpass1_setundef([ (Pred, Arity, Val) | Rest ], Nout, Nin) :-
  185.     $conlength(Pred, L), 
  186.     Nmed is Nin + L + 6,
  187.     (var(Val) -> Val = -2 ; true),
  188.     $asmpass1_setundef(Rest, Nout, Nmed).
  189.  
  190. /*    Fill in the values of any predicates which are referenced and 
  191. defined within this module.  Append the names of any predicates which
  192. are defined but not referenced by this module.  Ignore predicates with
  193. arity < 0, these are strictly internal jumps that should not go via the
  194. symbol table.                                */
  195.  
  196. $asmpass1_fillin([], _).
  197. $asmpass1_fillin([ (Name, Arity, LcValue) | Rest ], Table) :-
  198.     $member1((Name, Arity, LcValue), Table),
  199.     $asmpass1_fillin(Rest, Table).
  200.  
  201. /*    Uniq creates a list (Ulist) containing only the first label
  202. encountered for each predicate-name arity pair and excludes all labels
  203. whose predicate-name arity pair have an arity of -1.  Labels with an
  204. arity of -1 are used as the targets of jump instructions only.    */
  205.  
  206. $asm_uniq([],_).
  207. $asm_uniq([ ((Pname, Arity,_),_) | Tail], Ulist) :-
  208.     (Arity = -1 ;                /* internal branch */
  209.      $member2((Pname, Arity, _), Ulist)),   /* already present in ST */
  210.     $asm_uniq(Tail, Ulist).
  211. $asm_uniq([ ((Pname,Arity,_),Lc) | Tail], Ulist) :-
  212.     $member1((Pname, Arity, Lc), Ulist),
  213.     $asm_uniq(Tail, Ulist).
  214.  
  215.  
  216. /* end asmpass11.P *********************************************/
  217.